<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/layoutnew.css"/>
    <style>
        body {
            font-family: "Microsoft YaHei", 微软雅黑;
            font-size: 10px;
            background: url(../images/bac.png) no-repeat 50% 50% fixed;
            background-size: 100% 100%;
        }

        .open-navigation {
            position: absolute;
            top: 20px;
            left: 20px;
            width: 200px;
            text-decoration: none;
            border-bottom: none;
            text-align: left;
            z-index: 1;
        }

        .open-navigation i {
            padding: 10px;
            font-size: 20px;
            border-radius: 4px;
            background-color: #fff;
            width: 40px;
            height: 40px;
            text-align: center;
            float: left;
        }


    </style>
</head>
<body>




<div class="top_hongguan">


    <div style="width: 100%;height: 550px;position: relative;overflow:hidden">


        <div class="renxuanze">
            <div class="line">
                <div class="leftj"></div>
                <div class="xuanzj">&nbsp;</div>
                <div class="rightj"></div>
            </div>

            <ul id="dw-top-content" class="myxx">
                <li>
                    <a href="#" id="top-name-message-content" data-code="0"
                       onclick="clickTop(this)"> </a>
                </li>
            </ul>

        </div>

        <div class="resizable resizable2">
            <div class="rightbiaodan" id="section1" style="min-height: 500px">
                <div id="nextBtn" class="next-button" onclick="clickNextBtn()"></div>
                <div id="prevBtn" class="previous-button" onclick="clickPrevBtn()"></div>
                <div id="container">
                    <div class="section active" id="section0" style="min-height: 500px">
                        <div class="biaocont">
                            <table id="table" border="0" cellpadding="0" cellspacing="0" class="table2">

                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <img src="../image/backcome.png" height="30" width="30"
         style="color: #0062cc;position: fixed;bottom: -5px;z-index: 99" onclick="openleftfrm()"/>
</div>


<div id="nianbwapper" class="nianbwapper"
     style="width: 40%;float: left; background-color: #00134b; z-index: 99; left: -40%;">
    <div class="divtuoz">

        <div class="resizable resizable1" style="height: 106%;width:100%;margin-top: -40px;">
            <div class="leftnav">
                <div id="menu-content" class="menu"
                     style="height: 120%;width:100%;overflow-y: auto; min-height: 520px;">


                </div>
            </div>
        </div>

    </div>
</div>


<script id="menu-template" type="text/x-dot-template">
    <h4 id="{{=it.TPID}}" style="color: white">{{=it.NAME}}</h4>
    <ul id="tpid_{{=it.TPID}}" data-tpid="{{=it.TPID}}" class="lv_1">

    </ul>
</script>

<script id="lv_2-template" type="text/x-dot-template">
    <li>
        <a href="#" title="{{=it.NAME}}" class="lv_2_item" data-tpid="{{=it.TPID}}" data-rp-table-id="{{=it.RPTABLEID}}"
           onclick="clickTable(this)">{{=it.NAME}}</a>
    </li>
</script>

<script id="dw-top-template" type="text/x-dot-template">
    <li>
        <a href="#" data-code="{{=it.ID}}" style="font-size: 12px" onclick="clickTop(this)">{{=it.NAME}}</a>
    </li>
</script>

<script id="dw-bottom-template" type="text/x-dot-template">
    <li>
        <a href="#" data-code="{{=it.ID}}" style="font-size: 12px" onclick="clickBottom(this)">{{=it.NAME}}</a>
    </li>
</script>

<script id="dw-list-template" type="text/x-dot-template">
    <li>
        <a href="#" data-code="{{=it.ID}}" style="font-size: 12px" onclick="clickList(this)">{{=it.NAME}}</a>
    </li>
</script>

<script id="table-template" type="text/x-dot-template">

</script>
</body>

<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/echarts.min.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/jquery.min.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/convertToPinyin.js"></script>
<script type="text/javascript" src="../script/leek-common.js"></script>
<script type="text/javascript" src="../script/map/citys/city_map.js"></script>
<script type="text/javascript" src="../script/countup.js"></script>
<script type="text/javascript" src="../script/scriptabxz.js"></script>
<script type="text/javascript" src="../script/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../script/jquery1.10.4-ui.min.js"></script>
<script type="text/javascript" src="../script/lodash.js"></script>
<script type="text/javascript" src="../script/pageswitch.js"></script>
<script type="text/javascript" src="../script/tuozhai.js"></script>

<script type="text/javascript">

    var place_code = 0
    var RPTABLEID = null;

    var topname = ''

    var place_array = []

    var topnamesql = "select name from rs_unmk where id='0'"

    var menu_index = 0


    apiready = function () {

        selectSql(topnamesql, function (ret, err) {
            if (ret.status) {
                topname = ret.data[0].NAME

                place_array.push({NAME: topname, ID: 0})

                $("#top-name-message-content").text(topname);
            }
        })

        selectSql(list_level_1_sql, function (ret, err) {
            consoledebug.log('selectSql list_level_1_sql ret is : ' + JSON.stringify(ret))
            consoledebug.log('selectSql list_level_1_sql err is : ' + JSON.stringify(err))

            if (ret.status) {
                var data = ret.data

                for (var i = 0; i < data.length; i++) {
                    consoledebug.log('loadMenuHtml data i : ' + JSON.stringify(data[i]))
                    loadMenuHtml(data[i])
                }

                $(".menu > ul").eq(0).show()
                $(".menu h4").next().first().find('a').first().addClass('active')
                $(".menu h4").click(function () {
                    var className = $(this).attr('class')
                    $(this).parent().find('h4').removeClass("active")
                    if ("active" != className) {
                        $(this).addClass("active")
                    }

                    $('.lv_2_item').removeClass('active')
                    $(this).next().find('a').first().addClass('active')

                    //这是ul收缩效果
                    $(this).next().stop().slideToggle()
                    $(this).siblings().next("ul").stop().slideUp()
                })

                var lv_1_list = $('.lv_1')

                consoledebug.log('lv_1_list length : ' + lv_1_list.length)

                $('.lv_1').each(function () {
                    var tpid = $(this).attr('data-tpid')

                    consoledebug.log('tpid is : ' + tpid)

                    var db = api.require('db');
                    var ret = db.selectSqlSync({
                        name: db_name,
                        sql: converTPIDSql(tpid)
                    })

                    consoledebug.log('selectSqlSync ret is : ' + JSON.stringify(ret))
                    consoledebug.log('selectSqlSync err is : ' + JSON.stringify(err))

                    var data = ret.data

                    for (var i = 0; i < data.length; i++) {

                        loadLv2Html(tpid, data[i])
                    }
                })

                RPTABLEID = $('.lv_1').first().find('li').first().find('a').attr('data-rp-table-id')

                consoledebug.log('RPTABLEID is : ' + RPTABLEID)

                $('.lv_1').first().find('li').first().find('a').trigger("click")

                //   clickConfirm("two", topname)
                // loadDataRender()
            }
        })

        api.addEventListener({
            name: 'cleanarr'
        }, function (ret, err) {
            cleanarr()
        })


        api.addEventListener({
            name: 'swiperight'
        }, function (ret, err) {
            $('#nianbwapper').animate({
                'left': '0%',
            })
        })

        api.addEventListener({
            name: 'swipeleft'
        }, function (ret, err) {
            $('#nianbwapper').animate({
                'left': '-40%',
            })
        })

        api.addEventListener({
            name: 'loadnew3'
        }, function (ret, err) {
            consoledebug.log('addEventListener >>> OPENDB ret is : ' + JSON.stringify(ret))
            clickConfirm("two", topname)
        })


        api.addEventListener({
            name: 'frame3name'
        }, function (ret, err) {

            place_code = ret.value.code
            place_name = ret.value.place_name
            place_array = ret.value.place_arr

            //判断顶部城市是否改变 不是切换时刷新界面 0不刷 1刷新
            estimateCity(place_name,topname)

            clickConfirm(place_code, place_name)
        })

//        $('#section0').height(500)
//        $('#section1').height(500)
    }

    function estimateCity(cityname1,cityname2) {
        var citynum
        if(cityname1 == cityname2){
            citynum = 0
        }else {
            citynum = 1
        }
        api.sendEvent({
            name: 'frame3',
            extra: {
                data:citynum
            }
        });
    }



    function openleftfrm() {
        $('#nianbwapper').animate({
            'left': '0%'
        })
    }


    var list_level_1_sql = 'SELECT NAME, TPID FROM GWY_RPT_TP ORDER BY SORDER ASC, TPID ASC;'

    function converTPIDSql(TPID) {
        var sql = "SELECT RPTABLEID,NAME,TPID FROM GWY_RPT_CFG  WHERE TPID='" + TPID + "' ORDER BY SORDER ASC,RPTABLEID ASC"

        return sql
    }

    function converPlaceWithCodeSql(place_code) {
        var sql = "SELECT ID,NAME FROM RS_UNMK WHERE SID='" + place_code + "'"

        return sql
    }

    function converRPTABLEIDWithCodeSql(RPTABLEID, place_code) {
        var sql = "SELECT * FROM LD_" + RPTABLEID + " WHERE UNID = '" + place_code + "' ORDER BY ROWSID ASC"

        return sql
    }

    function loadMenuHtml(data) {
        var interText = doT.template($("#menu-template").text())
        $("#menu-content").append(interText(data))
    }

    function loadLv2Html(id, data) {
        var interText = doT.template($("#lv_2-template").text())
        $("#tpid_" + id).append(interText(data))
    }

    function loadTableHtml() {
        var interText = doT.template($("#table-template").text())
        $("#table").html(interText())
    }


    var _place_array = null

    function getDW(code) {
        $('#dw-list-content').empty()

        selectSql(converPlaceWithCodeSql(code), function (ret, err) {
            consoledebug.log('selectSql converPlaceWithCode ret is : ' + JSON.stringify(ret))
            consoledebug.log('selectSql converPlaceWithCode err is : ' + JSON.stringify(err))

            if (ret.status) {
                var data = ret.data

                for (var i = 0; i < data.length; i++) {
                    loadListHtml(data[i])
                }

                $('#danxuanqu').slideDown()
            } else {

            }
        })
    }

    function clickTop(el) {


        var name = $(el).text()
        var code = $(el).attr('data-code')


        var item = {
            place_array: place_array,
            name: name,
            code: code,
            type: 3
        }
        comOpenFrame('filtercity', './echartsframelist/', false, item, 3);


    }


    function clickList(el) {
        var name = $(el).text()
        var code = $(el).attr('data-code')

        var param = {
            NAME: name,
            ID: code
        }

        place_array.push(param)

        consoledebug.log('clickList place_array is : ' + JSON.stringify(place_array))

        $('#dw-bottom-content').empty()

        for (var i = 0; i < place_array.length; i++) {
            loadBottomHtml(place_array[i])
        }

        getDW(code)
    }

    function clickConfirm(ID, NAME) {


        if (ID == 'two') {

            setTimeout(function () {
                $('#dw-top-content').empty()
                place_array = [
                    {
                        NAME: topname,
                        ID: 0
                    }
                ]

                for (var i = 0; i < place_array.length; i++) {
                    loadTopHtml(place_array[i])
                }

            }, 300);

            place_code = 0
            place_name = topname


        } else {
            place_code = ID
            place_name = NAME
        }


        consoledebug.log('clickList place_array is : ' + JSON.stringify(place_code))
        $('#dw-top-content').empty()

        $("#danwei-message-content").empty()

        for (var i = 0; i < place_array.length; i++) {
            loadTopHtml(place_array[i])
        }

        consoledebug.log('place_code is : ' + place_code)

        $('#danxuanqu').slideUp()

        $('#place_name').text(place_name)
        $('#table_place_name').text(place_name)

        consoledebug.log('place_code is : ' + place_code)
        consoledebug.log('place_name is : ' + place_name)


        loadDataRender()
    }

    function clickCancel() {
        $('#danxuanqu').slideUp()

        place_array = _.clone(_place_array)
    }

    function loadTopHtml(data) {
        var interText = doT.template($("#dw-top-template").text())
        $("#dw-top-content").append(interText(data))
    }

    function loadBottomHtml(data) {
        var interText = doT.template($("#dw-bottom-template").text())
        $("#dw-bottom-content").append(interText(data))
    }

    function loadListHtml(data) {
        var interText = doT.template($("#dw-list-template").text())
        $("#dw-list-content").append(interText(data))
    }

    function getObjKeys(obj) {

        var keys = Object.keys(obj)
        return keys
    }

    function loadDataRender(type) {

        if (!judgeIsAnyNullStr(type)) {
            RPTABLEID = type
        }

        showProgress()

        selectSql(converRPTABLEIDWithCodeSql(RPTABLEID, place_code), function (ret, err) {
            consoledebug.log('RPTABLEID_place_code is : ' + RPTABLEID + '_' + place_code + ' ret is : ' + JSON.stringify(ret))
            consoledebug.log('RPTABLEID_place_code is : ' + RPTABLEID + '_' + place_code + ' err is : ' + JSON.stringify(err))



            var template = api.readFile({
                sync: true,
                path: 'widget://html/template/' + RPTABLEID + '.html'
            })

            consoledebug.log('template is : ' + JSON.stringify(template))

            $('#table-template').text(template)

            loadTableHtml()

            var data = ret.data


            if (judgeIsAnyNullStr(data)) {
                data = []
            }



            for (var i = 0; i < data.length; i++) {

                var name_array = getObjKeys(data[i])

                var j_array = []

                for (var j = 0; j < name_array.length; j++) {
                    if (name_array[j].indexOf('J') >= 0) {
                        j_array.push(name_array[j])
                    }
                }
                var clone_j_array = clone(j_array)


                for (var k = 0; k < j_array.length; k++) {
                    j_array[k] = j_array[k].replace(/J/, '')
                }


                for (var l = 0; l < j_array.length; l++) {
                    $('.datacls').each(function () {
                        if ($(this).text().trim() === '#R' + data[i].ROWSID + '#C' + j_array[l]) {
                            var j_name = clone_j_array[l]
                            //直统报表如果为0就不显示
                            if (data[i][j_name] == 0) {
                                $(this).text('')
                            } else {
                                $(this).text(data[i][j_name])
                            }
                        }
                    })
                }
            }


            if (data.length <= 0) {
                $('.datacls').each(function () {
                    if ($(this).text().trim().substring(0, 2) == '#R') {
                        $(this).text('')
                    }
                })
            }


            hideProgress()
        })
    }


    function clickTable(el) {
        RPTABLEID = $(el).attr('data-rp-table-id')
        consoledebug.log('RPTABLEID is : ' + RPTABLEID)

        $('.lv_2_item').removeClass('active')
        $(el).addClass('active')

        var lv2_item = $('.lv_2_item')

        consoledebug.log('lv2_item length is : ' + lv2_item.length)

        lv2_item.each(function (index, item) {
            if ($(item).attr('data-rp-table-id') === $(el).attr('data-rp-table-id')) {
                consoledebug.log('index is : ' + index)
                menu_index = index
                return false
            }
        })


        loadDataRender()

    }





    document.getElementById('table').addEventListener('click', function (e) {
        if ('datacls' == e.target.tagName.toLowerCase() || 'datacls' == e.target.className) {
            var rowid = e.target.getAttribute("rowid");
            var colid = e.target.getAttribute("colid");
            var val = $("#" + e.target.id).text()
            consoledebug.log('table click val: ' + JSON.stringify(val) + ':colid:' + JSON.stringify(colid))
            clickopenpeo(colid, rowid, '', val)
        }
    })

    var rossidlist = []

    var colsidlist = []

    var peosqllist = []
    var sqlthree = null


    var colrowidnumber

    var number

    var sqlwhereone



    function clickopenpeo(colsid, rowsid, name, val, ID) {

        colrowidnumber = 0
        number = 0



        rossidlist = rowsid.split(",");

        colsidlist = colsid.split(",");

        colrowidnumber = parseInt(rossidlist.length) * parseInt(colsidlist.length)


        var sql_arr = []
        if (val != 0) {

            for (i = 0; i < rossidlist.length; i++) {
                for (a = 0; a < colsidlist.length; a++) {
                    selectSql(converFcrySql(colsidlist[a], rossidlist[i]), function (ret, err) {
                        var sql = ret.data[0].DRILLSQL;
                        var newsql = sql.replace(/\"/g, "'");
                        var sqlone = newsql.substring(newsql.lastIndexOf("@FIELDS@") + 9, newsql.lastIndexOf("AND"));

                        var pjsqlone = sqlone.substring(0, sqlone.lastIndexOf("AND") + 3);
                        //   pjsqltwo = newsql.substring(newsql.lastIndexOf(")") + 1, newsql.lastIndexOf(","));

                        sqlwhereone = newsql.substring(newsql.lastIndexOf("@FIELDS@") + 9, newsql.lastIndexOf("WHERE") + 6);

                        var sqlwhere = sqlone.substring(sqlone.lastIndexOf("WHERE") + 6, sqlone.lastIndexOf("AND"));


                        sql_arr.push(sqlwhere)

                        number++
                        if (number == colrowidnumber) {

                            var comsql = ''
                            for (i = 0; i < sql_arr.length; i++) {
                                comsql += "(" + sql_arr[i] + ") OR "

                            }
                            comsql = "(" + comsql.substr(0, comsql.length - 3) + ") and"

                            if (judgeIsAnyNullStr(ID)) {
                                sqlthree = "select RS_A01.ID ID, A0101, (CASE WHEN A0104 = 1 THEN '男'else'女'end) A0104, age, a0140, (SELECT NAME FROM DIC_RS_GB3304 WHERE CODE = A0117) A0117, A0192, (SELECT NAME FROM DIC_rs_Zb09 where code = A0172) A0172 " + sqlwhereone + comsql + " (RS_A01.NID like '" + place_code + "%') " + "order by rs_a01.aorder "
                            } else {
                                sqlthree = "select RS_A01.ID ID, A0101, (CASE WHEN A0104 = 1 THEN '男'else'女'end) A0104, age, a0140, (SELECT NAME FROM DIC_RS_GB3304 WHERE CODE = A0117) A0117, A0192, (SELECT NAME FROM DIC_rs_Zb09 where code = A0172) A0172 " + sqlwhereone + comsql + " (RS_A01.NID like '" + ID + "%') " + "order by rs_a01.aorder "

                            }
                            var item = {
                                sql: sqlthree,
                                name: name
                            }
                            comOpenWin('dwpeolist_win', './dwlist/', item, 2);

                        }


                    })
                }
            }

        }
    }



    function cleanarr() {
        peosqllist = []
        sqlthree = null
    }



    function converFcrySql(colsid, rowsid) {
        var sql = "SELECT * FROM GWY_RPT_LD_NODESQL WHERE  ROWSID='" + rowsid + "' AND COLSID='" + colsid + "' AND RPTABLEID='JT2017001';"
        return sql
    }

    function clickNextBtn() {
        var length = $('.menu a').length

        if (menu_index >= length) {
            showToast("已是最后一页", "middle");


        } else {
            menu_index = menu_index + 1
            $('.lv_2_item').removeClass('active')
            $('.menu li a').eq(menu_index).addClass('active')
            RPTABLEID = $('.menu a').eq(menu_index).attr('data-rp-table-id')

            $('.menu a').eq(menu_index -1).css('color', '#C1C1E4')
            $('.menu a').eq(menu_index).css('color', 'white')
            loadDataRender()


        }
    }

    function clickPrevBtn() {
        if (menu_index <= 0) {

            showToast("已是第一页", "middle");
        } else {
            menu_index = menu_index - 1
            $('.lv_2_item').removeClass('active')
            $('.menu li a').eq(menu_index).addClass('active')
            RPTABLEID = $('.menu a').eq(menu_index).attr('data-rp-table-id')

            $('.menu a').eq(menu_index +1).css('color', '#C1C1E4')
            $('.menu a').eq(menu_index).css('color', 'white')
            loadDataRender()


        }
    }




</script>
</html>